<template>
	<view class="search">
		<view class="title">
			<u-icon name="arrow-left" size="24" @click="back"></u-icon>
			<text style="margin-left: 24rpx;">搜索</text>
		</view>
		<view class="searchbox">
			<view class="searchboxitem">
				<u--input placeholder="请输入姓名搜索" v-model="searchValue" prefixIcon="search"
					prefixIconStyle="font-size: 22px;color: #909399" shape="circle"></u--input>
				<text class="searchtext" @click="getStudentLists"> 搜索</text>
			</view>
			<view class="quxiao" @click="handleQuxiao">
				取消
			</view>
		</view>
		<view class="searchtabs">
			<u-tabs :list="tabsData" lineWidth="0" :activeStyle="{
				 width:'172rpx',
				 height:'60rpx',
			     color: '#FFFFFF',
				background: '#4A9CFF',
			
			    fontWeight: 'bold',}" :inactiveStyle="{
						width:'172rpx',
						height:'60rpx',
			            color: '#606266',
						
						background: '#ffffff',
			        }" itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;" @click="handleTabs">
			</u-tabs>
		</view>
		<view v-if="show" class="historydata">

			<view class="historytitle">
				<view class="text">
					搜索历史
				</view>
				<view class="del" @click="handleDelHis">
					<image class="deleteimg" src="http://sssx.nanjingyunpeng.cn/uploads/tupian/image/delete.png">
					</image>
				</view>
			</view>
			<view class="historydatacontent">
				<view class="hisItem" v-for="(item,index) in historySearchData" @click="handleHistory(item)">
					{{item}}
				</view>
			</view>
		</view>
		<view v-else class="searchContent">


			<view class="studentbox">
				<view class="boxitem" v-for="(item,index) in studentData" @click="gotoStudentdetail(item.id)">
					<view class="itemleft">
						<view class="avaterbox">
							<image class="avatartimg" :src="item.photos.url"></image>
						</view>
						<view class="nameandmessage">
							<view class="name">
								{{item.name}}
							</view>
							<view class="message">
								{{item.school}}/{{item.speciality}}
							</view>
						</view>
					</view>

					<view class="itemright">
						<view class="baokao">
							报考：
						</view>
						<view class="zhuanye">
							{{item.science_id_text}}
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		science,
		getStudentList
	} from "@/api/api.js"
	export default {
		data() {
			return {
				show: true,
				searchValue: "",
				tabsData: [],
				studentData: [{
					id: "1",
					name: "张宏建",
					message: "南院/物理系/研2班",
					zhuanye: "电子商务",
					avatar: "http://sssx.nanjingyunpeng.cn/uploads/tupian/image/studentsavayar.png"
				}, {
					id: "2",
					name: "司蒙蒙",
					message: "南院/物理系/研2班",
					zhuanye: "电子商务",
					avatar: "http://sssx.nanjingyunpeng.cn/uploads/tupian/image/studentsavayar.png"
				}],
				zhuanyeId: "",
				historySearchData: [],
			}
		},
		onLoad() {
			this.getscience()
			this.historySearchData = JSON.parse(uni.getStorageSync("historySearch"))
		},
		methods: {
			async getscience() {
				let res = await science()
				if (res.code == 1) {
					console.log("专业列表", res)
					this.tabsData = res.data
					this.zhuanyeId = res.data[0].id
					console.log("专业id", this.zhuanyeId)
					// this.getStudentLists()
				}
			},
			handleTabs(item) {
				console.log("item", item)
				this.zhuanyeId = item.id
				this.getStudentLists()
			},
			handleHistory(item){
				this.searchValue = item
				this.getStudentLists()
			},

			async getStudentLists() {
				if (this.searchValue == '') {
					uni.showToast({
						title: '请输入搜索内容！',
						icon: "none",
						duration: 1000
					});
					return
				}

				let res = await getStudentList({
					science_id: this.zhuanyeId,
					name: this.searchValue,
					page: 1,
					size: 20
				})
				if (uni.getStorageSync("historySearch")) {
					let data = JSON.parse(uni.getStorageSync("historySearch"))
					let flag = true
					data.forEach((item,index)=>{
						console.log(item,this.searchValue)
						if(item == this.searchValue ){
							flag = false
						}
					})
					console.log("data", data)
					if(flag){
						data.push(this.searchValue)
						uni.setStorageSync("historySearch", JSON.stringify(data))
					}
					
				} else {
					let dataArr = [this.searchValue]
					uni.setStorageSync("historySearch", JSON.stringify(dataArr))
				}
				this.historySearchData = JSON.parse(uni.getStorageSync("historySearch"))
				if (res.code == 1) {
					this.studentData = res.data
					if (res.data.length > 0) {
						this.show = false
					}
					// this.studentData.forEach((item, index) => {
					// 	item.science_ids = item.science_ids.split(",")
					// })
					if (res.data.length == 0) {
						uni.showToast({
							title: '本专业暂无该学生，请重新搜索！',
							icon: 'none',
							duration: 1000
						})
					}
					console.log("搜索内容", res)
				}
			},
			handleQuxiao() {
				this.back()
				// this.searchValue = ''
				// this.studentData = []
				
			},
			handleDelHis() {
				let that = this
				uni.showModal({
					// title: '提示',
					content: '确定删除全部历史记录？',
					confirmColor: '#4A9CFF',
					success: async function(res) {
						console.log("是否选择", res)
						if (res.confirm) {
							uni.removeStorageSync("historySearch")
							that.historySearchData = []
							uni.showToast({
								title: '已删除历史搜索记录',
								icon: 'none',
								duration: 1000
							})
						}
					}
				})
			},
			gotoStudentdetail(id) {
				uni.navigateTo({
					url: `../studentDetail/index?id=${id}`
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});

			}
		}
	}
</script>

<style lang="scss" scoped>
	.searchtabs {
		width: 100%;
		height: 60rpx;
		margin-top: 30rpx;

		/deep/.u-tabs__wrapper__nav__item__text {
			border-radius: 30rpx !important;
			text-align: center;
			line-height: 60rpx !important;
		}

	}

	.historydatacontent {
		display: flex;
		margin-top: 10rpx;
		width: 100%;
		flex-wrap: wrap;

		.hisItem {
			// min-width: 180rpx;
			height: 68rpx;
			background: #FFFFFF;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			opacity: 1;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 69rpx;
			text-align: center;
			margin-right: 10rpx;
			margin-bottom: 10rpx;
			padding: 0 10rpx;

		}
	}

	.search {
		min-height: 100%;
		background-color: #F7F8FA;

		.title {
			width: 100%;
			height: 176rpx;
			padding-top: 110rpx;
			box-sizing: border-box;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 38rpx;
			padding-left: 34rpx;
			box-sizing: border-box;

		}

		.searchbox {
			padding: 30rpx;
			padding-bottom: 15rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #ffffff;

			.searchboxitem {
				width: 620rpx;
				background: #F7F8FA;
				border-radius: 200rpx 200rpx 200rpx 200rpx;
				position: relative;

				.searchtext {
					position: absolute;
					right: 5rpx;
					top: 4.5rpx;
					width: 156rpx;
					height: 60rpx;
					background: #4A9CFF;
					border-radius: 200rpx 200rpx 200rpx 200rpx;
					opacity: 1;
					display: inline-block;
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					line-height: 42rpx;
					text-align: center;
					line-height: 60rpx;
					z-index: 99;
				}
			}

			.quxiao {
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 45rpx;
			}
		}

		.historydata {
			width: 100%;
			padding: 30rpx;
			box-sizing: border-box;


			.historytitle {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.text {
					font-size: 32rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #000000;
					line-height: 38rpx;
				}

			}

			.del {
				.deleteimg {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}

		.searchContent {
			width: 100%;
			// background-color: #F7F8FA;
			padding-left: 30rpx;
			box-sizing: border-box;
			margin-top: 30rpx;



			.searchcontentData {
				width: 100%;
				padding: 30rpx;
				box-sizing: border-box;
				margin-top: 30rpx;
				display: flex;
				justify-content: space-between;

				.searchcontentDataitem {
					width: 336rpx;
					height: 562rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 0rpx 24rpx 0rpx rgba(206, 220, 237, 0.2);
					border-radius: 30rpx 30rpx 30rpx 30rpx;

					.imgbox {
						width: 336rpx;
						height: 360rpx;

						.tearchtimg {
							width: 100%;
							height: 100%;
						}

					}

					.name {
						font-size: 32rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						line-height: 50rpx;
						margin-left: 20rpx;
					}

					.zhicheng {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						line-height: 40rpx;
						margin-left: 20rpx;
					}

					.zhuanye {
						margin-left: 20rpx;
						display: flex;

						.zhichengitem {
							padding: 0 3rpx;

							height: 48rpx;
							background: #F0F7FF;
							border-radius: 12rpx 12rpx 12rpx 12rpx;
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #4A9CFF;
							line-height: 28rpx;
							text-align: center;
							line-height: 48rpx;
							margin-right: 10rpx;
						}
					}

				}
			}


		}

		.studentbox {
			width: 100%;
			// height: 300rpx;
			margin-top: 30rpx;

			.boxitem {
				width: 690rpx;
				height: 164rpx;
				background: #FFFFFF;
				border-radius: 30rpx 30rpx 30rpx 30rpx;
				margin-bottom: 30rpx;
				display: flex;
				justify-content: space-between;
				padding: 30rpx;
				box-sizing: border-box;

				.itemleft {
					display: flex;

					.avaterbox {
						.avatartimg {
							margin-right: 10rpx;
							width: 104rpx;
							height: 104rpx;
							border-radius: 100rpx;
						}
					}

					.nameandmessage {
						padding-top: 5rpx;
						box-sizing: border-box;

						.name {
							font-size: 32rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							line-height: 38rpx;
						}

						.message {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							line-height: 30rpx;
							margin-top: 20rpx;
						}
					}

				}

				.itemright {
					display: flex;
					align-items: center;
					flex-direction: column;
					justify-content: space-between;

					.baokao {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						line-height: 30rpx;
					}

					.zhuanye {
						height: 48rpx;
						background: #F0F7FF;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
						opacity: 1;
						border: 2rpx solid #4A9CFF;
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #4A9CFF;
						line-height: 28rpx;
						text-align: center;
						line-height: 48rpx;

					}
				}
			}
		}
	}
</style>